﻿using System;
using System.Text.RegularExpressions;

namespace Soul.Expressions
{
    internal static class LambdaConstant
    {
        public static bool TryNULL(string token, out object value)
        {
            if (token == "null")
            {
                value = null;
                return true;
            }

            value = null;
            return false;
        }
        public static bool TryString(string token, out string value)
        {
            var match = Regex.Match(token, "^@?\"(?<data>[^\"\\\\]*(\\\\.[^\"\\\\]*)*)?\"$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = data;
                return true;
            }
            value = null;
            return false;
        }
        public static bool TryBoolean(string token, out bool value)
        {
            var match = Regex.Match(token, "^(?<data>true|false)$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = Convert.ToBoolean(data);
                return true;
            }
            value = false;
            return false;
        }
        public static bool TryInt64(string token, out long value)
        {
            var match = Regex.Match(token, "^(?<data>-?[0-9]+)[lL]$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = Convert.ToInt64(data);
                return true;
            }
            value = 0;
            return false;
        }
        public static bool TryDecimal(string token, out decimal value)
        {
            var match = Regex.Match(token, @"^(?<data>-?[0-9]+\.?[0-9])[mM]$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = Convert.ToDecimal(data);
                return true;
            }
            value = 0;
            return false;
        }
        public static bool TryFloat(string token, out float value)
        {
            var match = Regex.Match(token, @"^(?<data>-?[0-9]+\.?[0-9])[fF]$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = Convert.ToSingle(data);
                return true;
            }
            value = 0;
            return false;
        }
        public static bool TryDouble(string token, out double value)
        {
            var match = Regex.Match(token, @"^(?<data>-?[0-9]+\.?[0-9])[dD]{0,1}$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = Convert.ToDouble(data);
                return true;
            }
            value = 0;
            return false;
        }
        public static bool TryInt32(string token, out int value)
        {
            var match = Regex.Match(token, "^(?<data>-?[0-9]+)?$");
            if (match.Success)
            {
                var data = match.Groups["data"].Value;
                value = Convert.ToInt32(data);
                return true;
            }
            value = 0;
            return false;
        }
    }
}
